#pragma once

namespace ys
{
	template<class T>
	struct list_Node
	{
		list_Node<T>* _prev;
		list_Node<T>* _next;
		T _data;

		list_Node(const T& val = T())
			:_prev(nullptr)
			,_next(nullptr)
			,_data(val)
		{}
	};
	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef list_Node<T> Node;
		//                     <T, T&,  T*>
		typedef __list_iterator<T, Ref, Ptr> self;

		__list_iterator(Node* node)
			:_node(node)
		{}

		self operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			_node = _node->_next;
			return *this;
		}
		self operator--()
		{
			_node = _node->_prev; 
			return *this;
		}
		self operator--(int)
		{
			_node = _node->_prev;
			return *this;
		}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &(_node->_data);
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		} 
		Node* _node;
	};

	template<class Iterator, class Ref, class Ptr>
	struct ReverseIterator
	{
		typedef ReverseIterator<Iterator, Ref, Ptr> self;
		ReverseIterator(Iterator it)
			:_it(it)
		{}
		Ref operator*()
		{
			Iterator cur = _it;
			return *(--cur);
		}
		self operator++()
		{
			--_it;
			return *this;
		}
		self operator--()
		{
			++_it;
			return *this;
		}
		bool operator!=(const self& s)
		{
			return _it != s._it;
		}
		Iterator _it; 
	};
	template<class T /*, class Alloc = allocator<T>*/>
	class list
	{
	public:
		typedef list_Node<T> Node;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;

		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;
		//---------------------------Member functions---------------------//
		void Init_list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list(size_t n, const T& val = T())
		{
			Init_list();
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		list()
		{
			Init_list();
		}
		list(const list<T>& lt)
		{
			Init_list();
			for (auto e : lt)
			{
				push_back(e);
			}
		}
		list<T>& operator=(const list<T>& lt)
		{
			if (this != &lt)
			{
				list<T> tmp = lt;
				swap(tmp);
			}
			return *this;
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		//--------------------------Iterators:-----------------------------//
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin() const
		{
			return _head->_next;
		}
		const_iterator end() const
		{
			return _head;
		}

		reverse_iterator rbegin()
		{
			return end();
		}
		reverse_iterator rend()
		{
			return begin();
		}
		const_reverse_iterator rbegin() const
		{
			return end();
		}
		const_reverse_iterator rend() const
		{
			return begin();
		}
		//-----------------------------------Capacity:-------------------//
		bool empty() const
		{
			return _head->_next == _head;
		}
		size_t size() const
		{
			return _size;
		}
		//----------------------------Modifity-----------------------------//
		void push_back(const T& val)
		{
			insert(iterator(_head), val);
		}

		void pop_back()
		{
			erase(iterator(_head->_prev));
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		iterator insert(iterator pos, const T& val)
		{
			Node* prev = pos._node->_prev;
			Node* newnode = new Node(val);

			newnode->_next = pos._node;
			newnode->_prev = prev;

			prev->_next = newnode;
			pos._node->_prev = newnode;

			++_size;

			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur;
			return iterator(next);
		}
		//-------------------Non-member function overloads---------------//
		void swap(list<T>& tmp)
		{
			std::swap(_head, tmp._head);
			std::swap(_size, tmp._size);
		}
	private:
		Node* _head;
		size_t _size;
	};

	void test_list()
	{
		list<vector<int>> lt;
		lt.push_back(vector<int>(1));
		lt.push_back(vector<int>(2));

		
		list<vector<int>>::reverse_iterator it = lt.rbegin();
		while (it != lt.rend())
		{
			cout << &(it._it._node->_data)<< endl;
			++it;
		}
	}
}